package icu.lookyousmileface.mylock;

import jdk.nashorn.internal.objects.annotations.Where;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author starrysky
 * @title: MyLocks
 * @projectName Juc_Pro
 * @description: 使用CAS创建的自旋锁
 * @date 2021/1/309:16 下午
 */
public class MyLocks {
    private final  AtomicReference<Thread> locks = new AtomicReference<>();

    public void upLock() {
        System.out.println(Thread.currentThread().getName()+"=>上锁");
        //自旋锁
        while (!locks.compareAndSet(null,Thread.currentThread())){

        }
    }

    public void downLock() {
        System.out.println(Thread.currentThread().getName()+"=>解锁");
        locks.compareAndSet(Thread.currentThread(),null);
    }
}
